** This dofile makes the results related to changes in number of partners

/*
Table 4: Capability Cutoff Changes and Number of Partners B. Number of Partners

Table A3: Negative Degree Assortativity under Different Match Definitions
*/

** For Table 4, B. Number of Partners
* Caliculate exporters' capability and number of partners
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep if year==2004
drop if newclid2==.
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
bys hs6 rfc newclid2: gen pair=_n
bys hs6 rfc: egen pair2=total(pair)
collapse (sum) value_exporter_importer, by(hs6 rfc pair2)
gen value_per_importer = value_exporter_importer/pair2
gen capability=log(value_per_importer)
rename pair2 num_partner_04
drop value_exporter_importer value_per_importer 
save "${temp_data}hs6_exp_num_partner04.dta", replace

use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep if year==2007
drop if newclid2==.
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
bys hs6 rfc newclid2: gen pair=_n
bys hs6 rfc: egen pair2=total(pair)
collapse (mean) pair2, by(hs6 rfc)
rename pair2 num_partner_07
merge 1:1 hs6 rfc using "${temp_data}hs6_exp_num_partner04.dta"
drop if _merge!=3
drop _merge
gen partner_change=num_partner_07-num_partner_04
gen hs2=substr(hs6,1,2)
/*Make the sample consistent with the main regression sample*/
gen hs6_exporter_id=hs6+"_"+ rfc
merge 1:m hs6_exporter_id using "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta"
* Set iq_mex_bind80 as a Binding measure
rename iq_mex_bind80 Binding
drop if _merge!=3
drop _merge
merge m:1 hs6_exporter_id using "${output_data}exporter_rank.dta"
gen cap_bind=capability*Binding
keep if mv_rank_d0704!=.

eststo clear
eststo: xi:reg partner_change Binding i.hs2, cluster(hs6)


* Caliculate importers' capability and number of partners
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep if year==2004
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
bys hs6 rfc newclid2: gen pair=_n
bys hs6 newclid2: egen pair2=total(pair)
collapse (mean) pair2, by(hs6 newclid2)
rename pair2 num_partner_04
save "${temp_data}hs6_imp_num_partner04.dta", replace

use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
keep if year==2007
collapse (sum) value_exporter_importer, by(rfc newclid2 hs6)
bys hs6 rfc newclid2: gen pair=_n
bys hs6 newclid2: egen pair2=total(pair)
collapse (mean) pair2, by(hs6 newclid2)
rename pair2 num_partner_07
merge 1:1 hs6 newclid2 using "${temp_data}hs6_imp_num_partner04.dta"
drop if _merge!=3
drop _merge
gen partner_change=num_partner_07-num_partner_04
gen hs2=substr(hs6,1,2)
/*Make the sample consistent with the main regression sample*/

tostring newclid2, replace
gen hs6_importer_id=hs6+"_"+newclid2
destring newclid2, replace
merge 1:m hs6_importer_id using "${output_data}US_for_analysis_04_07_hs6_restrict.dta"
drop if _merge!=3
* Set iq_mex_bind80 as a Binding measure
rename iq_mex_bind80 Binding
drop _merge
keep if e_mv_rank_d0704!=.
merge m:1 hs6_importer_id using "${output_data}importer_rank.dta"

eststo: xi:reg partner_change Binding i.hs2, cluster(hs6)

esttab using "${output}number_partners/change_number_of_partners_0407.tex", replace nonumbers nocons indicate("Hs2-level Effects=_Ihs2*") starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(2)) se(par fmt(2)))  stats(N, fmt(%9.0f) labels(Obs.)) posthead( &\multicolumn{2}{c}{Change in Number of Partners}\\ \hline &Mexico&US\\ \hline) varlabels((d)) collabels(,none) substitute((d)) nomtitles




** For Table A3
* Firm level
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
collapse value_exporter_importer, by(year newclid2 rfc)
bysort year rfc: gen exporter=_n
replace exporter=0 if exporter!=1
bysort year rfc: gen buyersexporter=_N
replace buyersexporter=. if exporter==0
bysort year newclid2: gen sellers_importer=_N
egen mean_sellers_importer=mean(sellers_importer), by(rfc year)
drop if exporter==0
egen mean_buyers_exporter=mean(buyersexporter), by(year)
egen mean_mean_sellers_importer=mean(mean_sellers_importer), by(year)
gen lmean_sellers_importer=log(mean_sellers_importer/mean_mean_sellers_importer)
gen lbuyersexporter=log(buyersexporter/mean_buyers_exporter)

eststo clear
foreach year in "2004" "2007"{
eststo: reg lmean_sellers_importer lbuyersexporter if year==`year', robust
}

* Product level
use "${output_data}matched_exp_imp_hs6_junedecember_maquila_restrict.dta", clear
collapse value_exporter_importer, by(year newclid2 rfc hs6)
bysort year rfc hs6: gen exporter=_n
replace exporter=0 if exporter!=1
bysort year rfc hs6: gen buyersexporter=_N
bysort year newclid2 hs6: gen sellers_importer=_N
egen mean_sellers_importer=mean(sellers_importer), by(rfc year hs6)
drop if exporter==0
egen mean_buyers_exporter=mean(buyersexporter), by(year)
egen mean_mean_sellers_importer=mean(mean_sellers_importer), by(year)
gen lmean_sellers_importer=log(mean_sellers_importer/mean_mean_sellers_importer)
gen lbuyersexporter=log(buyersexporter/mean_buyers_exporter)

foreach year in "2004" "2007" {
eststo: reg lmean_sellers_importer lbuyersexporter if year==`year', cluster(hs6)
}

** With product FEs
foreach year in "2004" "2007" {
eststo: areg lmean_sellers_importer lbuyersexporter if year==`year', absorb(hs6) cluster(hs6)
}

esttab using "${output}number_partners/ndam_alternative_matching_def.tex", replace nonumbers nocons starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) stats(N, fmt(%9.0f) labels(Obs.)) posthead( &\multicolumn{6}{c}{Log average number of sellers per buyer}\\ \hline Definition of Match&\multicolumn{2}{c}{Firm-Level}&\multicolumn{4}{c}{Product-Level} &2004&2007&2004&2007&2004&2007\\ \hline &(1)&(2)&(3)&(4)&(5)&(6) \\ hline Product Fixed effects& -&-&No&No&Yes&Yes\\ \hline) varlabels((d)) collabels(,none) substitute((d)) nomtitles
